package com.zby.simulationHibernate.util.reflect;

import com.zby.simulationHibernate.constant.StandardBasicTypes;
import com.zby.simulationHibernate.util.common.CommonUtil;
import com.zby.simulationHibernate.util.exception.GenericException;
import com.zby.simulationHibernate.util.exception.PropertyNotFoundException;
import com.zby.simulationHibernate.util.reflect.property.PropertyAccessor;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created By zby on 15:56 2019/1/28
 * <p>
 * 属性的工具类
 */

public class PropertyUtil<T> {

    /**
     * Created By zby on 16:05 2019/1/28
     * 将属性名称转化为get方法
     *
     * @param type         属性类型
     * @param propertyName 属性名称
     */
    public static String propertyNameTransformerGetMethod(Type type, String propertyName) throws PropertyNotFoundException {
        if (CommonUtil.isNull(propertyName))
            throw new PropertyNotFoundException("属性名臣名称propertyName=" + propertyName, null, null);
        if (StandardBasicTypes.BOOLEAN.equals(type)) {
            return "is" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1, propertyName.length());
        }
        return "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1, propertyName.length());
    }

    /**
     * Created By zby on 13:57 2019/1/24
     * 封装方法名称
     */
    public static String propertyNameTransformerSetMethod(String fieldName) {
        if (CommonUtil.isNull(fieldName))
            throw new GenericException("属性名称为空");
        return "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length());
    }

    /**
     * Created By zby on 16:58 2019/1/28
     * 是否需要将属性名转化为数据库识别的字段名
     *
     * @param fieldName 属性名
     * @param flag      标志
     *                  true表示属性需要转换，比如 customerName  --> customer_name
     *                  false 表示不需要转换，比如 customerName  --> customerName
     */
    public static String propertyNameTransformColumnName(String fieldName, boolean flag) {
        if (flag) {
            //通过字段名来获取字段值
            Matcher matcher = Pattern.compile("[A-Z]").matcher(fieldName);
            while (matcher.find()) {
                return fieldName.replace(matcher.group(), "_" + matcher.group().toLowerCase());
            }
        }
        return fieldName;
    }

    /**
     * Created By zby on 13:58 2019/1/30
     * 类的属性是否包含id
     */
    public static String containId(Field[] fields) {
        if (CommonUtil.isNotNull(fields)) {
            for (Field field : fields) {
                if (field.getName().equalsIgnoreCase("id"))
                    return "id";
            }
        }
        return null;
    }

    /**
     * Created By zby on 14:49 2019/1/30
     * 属性的id值是否为空
     */
    public static <T> Long getIdValue(Field[] fields, T t, PropertyAccessor<T> propertyAccessor) {
        String id = containId(fields);
        if (CommonUtil.isNotNull(id)) {
            Long idValue = (Long) propertyAccessor.getPropertyValue(t, id);
            return CommonUtil.isNotNull(idValue) ? idValue : 0L;
        }
        return 0L;
    }
}
